package com.ftf.tp.blog.infrastructure.repository;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ftf.tp.blog.application.vo.UserInfoVO;
import com.ftf.tp.blog.domain.article.aggregate.ArticleAgg;
import com.ftf.tp.blog.domain.configuration.dos.ColumnDO;
import com.ftf.tp.blog.domain.configuration.repository.ColumnRepository;
import com.ftf.tp.blog.infrastructure.converter.ColumnConverter;
import com.ftf.tp.blog.infrastructure.mapper.ArticleColumnMapper;
import com.ftf.tp.blog.infrastructure.mapper.ColumnMapper;
import com.ftf.tp.blog.infrastructure.mapper.pos.ArticleColumn;
import com.ftf.tp.blog.infrastructure.mapper.pos.ColumnPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author tanghao
 * @date 2022/7/28
 */
@Slf4j
@Repository
public class ColumnRepositoryImpl implements ColumnRepository {


    @Resource
    private ColumnMapper columnMapper;

    @Resource
    private ArticleColumnMapper articleColumnMapper;

    /**
     * 新增专栏
     *
     * @param columnDO 专栏前端实体
     */
    @Override
    public void addColumnByUser(ColumnDO columnDO) {
//        Object object = userFeign.getUserInfo();
        UserInfoVO userInfo = JSON.parseObject(JSON.toJSONString(""), UserInfoVO.class);
        ColumnPO columnPo = ColumnConverter.INSTANCE.doToPo(columnDO);
        columnPo.setArticleNumber(0);
        columnPo.setSubscribeNumber(0);
        columnPo.setUserId(userInfo.getId());
        columnPo.setCreatedBy(userInfo.getId());
        columnPo.setUpdatedBy(userInfo.getId());
        columnMapper.insert(columnPo);
        log.info("用户{}新建专栏成功,专栏名称:{}", userInfo.getId(), columnPo.getColumnName());
    }

    /**
     * 新建文章 修改专栏信息 专栏内文章数+1
     *
     * @param columnDOList 专栏数据
     */
    @Override
    public void updateColumnByAddBlog(List<ColumnDO> columnDOList) {
        columnDOList.forEach(columnDO -> {
            //根据关系表获取专栏内文章数
            Integer number = columnMapper.selectColumnBlogNumber(columnDO.getId());
            columnDO.setBlogNumber(number);
            columnMapper.updateById(ColumnConverter.INSTANCE.doToPo(columnDO));
        });
    }

    /**
     * 根据userId获取指定的专栏信息
     *
     * @param userId 用户id
     */
    @Override
    public List<ColumnDO> getColumnByUserId(Long userId) {
        List<ColumnPO> columnPoList = columnMapper.selectList(
                new QueryWrapper<ColumnPO>().eq("user_id", userId));
        if (CollectionUtils.isNotEmpty(columnPoList)) {
            return ColumnConverter.INSTANCE.poToDoList(columnPoList);
        }
        return null;
    }

    @Override
    public List<ColumnDO> getColumnByIdList(List<Long> configureIdList) {
        List<ColumnPO> columnPoList = columnMapper.selectList(
                new QueryWrapper<ColumnPO>().in("id", configureIdList));
        return ColumnConverter.INSTANCE.poToDoList(columnPoList);
    }

    @Override
    public void articleSync(ArticleAgg articleAgg) {
        articleColumnMapper.batchInsert(articleAgg.getColumnValues().stream().map(columnValue -> ArticleColumn.builder()
                .articleId(articleAgg.getId())
                .columnId(columnValue.getId())
                .columnName(columnValue.getColumnName())
                .authorId(articleAgg.getArticleAuthorId())
                .authorName(articleAgg.getArticleAuthorName())
                .build()).collect(Collectors.toList()));
    }

    @Override
    public void articleNumUpdate(List<Long> columnIdList, Integer nums) {
        articleColumnMapper.articleNumUpdate(columnIdList,nums);
    }
}
